//
//  RecommendComicCell.swift
//  Spookiness
//
//  Created by 腾億 on 2017/12/7.
//  Copyright © 2017年 tml. All rights reserved.
//

import UIKit

enum RecommendComicCellStyle {
    case none
    case withTitle
    case withTitleAndDesc
}

class RecommendComicCell: UICollectionViewCell {
    
    fileprivate lazy var iconView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    fileprivate lazy var titleLB: UILabel = {
       let title = UILabel()
        title.textColor = UIColor.black
        title.font = UIFont.systemFont(ofSize: 14)
        return title
    }()
    
    fileprivate lazy var descLB: UILabel = {
       let desc = UILabel()
        desc.textColor = UIColor.gray
        desc.font = UIFont.systemFont(ofSize: 12)
        return desc
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        clipsToBounds = true
        self.contentView.addSubview(self.iconView)
        self.contentView.addSubview(self.titleLB)
        self.contentView.addSubview(self.descLB)
        setUpSnap()
    }
    
    fileprivate func setUpSnap() {
        self.titleLB.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(UIEdgeInsetsMake(0, 10, 0, 10))
            make.height.equalTo(25)
            make.bottom.equalToSuperview().offset(-10)
        }
        
        self.iconView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.bottom.equalTo(self.titleLB.snp.top)
        }
        
        self.descLB.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview().inset(UIEdgeInsetsMake(0, 10, 0, 10))
            make.height.equalTo(20)
            make.top.equalTo(self.titleLB.snp.bottom)
        }
    }
    
    var style: RecommendComicCellStyle = .withTitle {
        didSet {
            switch style {
            case .none:
                self.titleLB.snp.updateConstraints({ (make) in
                    make.bottom.equalToSuperview().offset(25)
                })
               self.titleLB.isHidden = true
                self.descLB.isHidden = true
            case .withTitle:
                self.titleLB.snp.updateConstraints({ (make) in
                    make.bottom.equalToSuperview().offset(-10)
                })
                self.titleLB.isHidden = false
                self.descLB.isHidden = true
            case .withTitleAndDesc:
                self.titleLB.snp.updateConstraints({ (make) in
                    make.bottom.equalToSuperview().offset(-25)
                })
                self.titleLB.isHidden = false
                self.descLB.isHidden = false
            }
        }
    }
    
    var model: ComicsModel? {
        didSet {
            guard model != nil else {
                return
            }
            iconView.kf.setImage(urlString: model?.cover, placeholder: (bounds.width > bounds.height) ? UIImage(named: "normal_placeholder_h") : UIImage(named: "normal_placeholder_v"))
            titleLB.text = model?.name ?? model?.title
            descLB.text = model?.subTitle ?? "更新至\(model?.content ?? "0")集"
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
